home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / edit / jed207.lha / src / regexp.lha / regsub.c < prev   
C/C++ Source or Header  |  1993-01-16  |  3KB  |  140 lines

  1. /*
  2.  * regsub @(#)regsub.c    1.3 of 2 April 86
  3.  *
  4.  * Copyright (c) 1986 by University of Toronto. Written by Henry Spencer.  Not
  5.  * derived from licensed software.
  6.  *
  7.  * Permission is granted to anyone to use this software for any purpose on any
  8.  * computer system, and to redistribute it freely, subject to the following
  9.  * restrictions:
  10.  *
  11.  * 1. The author is not responsible for the consequences of use of this
  12.  * software, no matter how awful, even if they arise from defects in it.
  13.  *
  14.  * 2. The origin of this software must not be misrepresented, either by explicit
  15.  * claim or by omission.
  16.  *
  17.  * 3. Altered versions must be plainly marked as such, and must not be
  18.  * misrepresented as being the original software.
  19.  */
  20.  
  21. /*
  22.  * CHANGED, 14-Jan-93, by J.Harper,
  23.  * added #ifdef __STDC__ prototype sections so I can use registerized
  24.  * arguments
  25.  *
  26.  * also, I added the regsublen() function for safety & general usefulness
  27.  * (regsub() has no checks for overstepping its dest string)
  28.  */
  29.  
  30. #include <stdio.h>
  31. #ifdef AMIGA
  32. #include "regexp.h"
  33. #else
  34. #include <regexp.h>
  35. #endif
  36. #include "regmagic.h"
  37.  
  38. #ifndef CHARBITS
  39. #define UCHARAT(p)    ((int)*(unsigned char *)(p))
  40. #else
  41. #define UCHARAT(p)    ((int)*(p)&CHARBITS)
  42. #endif
  43.  
  44. #ifdef __STDC__
  45. #include <string.h>
  46. void    regerror(char *);
  47. #endif
  48.  
  49. /*
  50.  * - regsub - perform substitutions after a regexp match
  51.  */
  52. void
  53. regsub(prog, source, dest)
  54.     regexp       *prog;
  55.     char       *source;
  56.     char       *dest;
  57. {
  58.     register char  *src;
  59.     register char  *dst;
  60.     register char   c;
  61.     register int    no;
  62.     register int    len;
  63.  
  64.     if (prog == NULL || source == NULL || dest == NULL) {
  65.     regerror("NULL parm to regsub");
  66.     return;
  67.     }
  68.     if (UCHARAT(prog->program) != MAGIC) {
  69.     regerror("damaged regexp fed to regsub");
  70.     return;
  71.     }
  72.     src = source;
  73.     dst = dest;
  74.     while ((c = *src++) != '\0') {
  75.     if (c == '&')
  76.         no = 0;
  77.     else if (c == '\\' && '0' <= *src && *src <= '9')
  78.         no = *src++ - '0';
  79.     else
  80.         no = -1;
  81.  
  82.     if (no < 0) {        /* Ordinary character. */
  83.         if (c == '\\' && (*src == '\\' || *src == '&'))
  84.         c = *src++;
  85.         *dst++ = c;
  86.     } else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
  87.         len = prog->endp[no] - prog->startp[no];
  88.         (void) strncpy(dst, prog->startp[no], len);
  89.         dst += len;
  90.         if (len != 0 && *(dst - 1) == '\0') {       /* strncpy hit NUL. */
  91.         regerror("damaged match string");
  92.         return;
  93.         }
  94.     }
  95.     }
  96.     *dst++ = '\0';
  97. }
  98.  
  99. /*
  100.  * - regsublen - dummy regsub() returning length of contructed string,
  101.  * including terminating '\0'
  102.  */
  103. int
  104. regsublen(prog, source)
  105.     regexp       *prog;
  106.     char       *source;
  107. {
  108.     register char  *src;
  109.     register char   c;
  110.     register int    no;
  111.     register int    dstlen = 1;
  112.  
  113.     if (prog == NULL || source == NULL) {
  114.     regerror("NULL parm to regsublen");
  115.     return(0);
  116.     }
  117.     if (UCHARAT(prog->program) != MAGIC) {
  118.     regerror("damaged regexp fed to regsublen");
  119.     return(0);
  120.     }
  121.     src = source;
  122.     while ((c = *src++) != '\0') {
  123.     if (c == '&')
  124.         no = 0;
  125.     else if (c == '\\' && '0' <= *src && *src <= '9')
  126.         no = *src++ - '0';
  127.     else
  128.         no = -1;
  129.  
  130.     if (no < 0) {        /* Ordinary character. */
  131.         if (c == '\\' && (*src == '\\' || *src == '&'))
  132.         c = *src++;
  133.         dstlen++;
  134.     } else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
  135.         dstlen += prog->endp[no] - prog->startp[no];
  136.     }
  137.     }
  138.     return(dstlen);
  139. }
  140.